//
//  MessageCell.swift
//  JKTools
//
//  Created by 姜奎 on 2/19/25.
//

import Cocoa

class MessageBaseCell: NSCollectionViewItem {

    let bubbleView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.cornerRadius = 12
        return view
    }()

    let messageLabel: NSTextField = {
        let label = NSTextField(labelWithString: "")
        label.maximumNumberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.textColor = .labelColor
        return label
    }()

    let timeLabel: NSTextField = {
        let label = NSTextField(labelWithString: "")
        label.font = .systemFont(ofSize: 10)
        return label
    }()

    override func loadView() {
        view = NSView()
        view.addSubview(bubbleView)
        bubbleView.addSubview(messageLabel)
        view.addSubview(timeLabel)

            // 设置自动布局约束
        bubbleView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        timeLabel.translatesAutoresizingMaskIntoConstraints = false
    }

    func configure(with message: MessageEntity) {
        messageLabel.stringValue = message.content.stringValue() ?? "??"
        timeLabel.stringValue = message.timestamp.formattedTodayTime
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        messageLabel.stringValue = ""
        timeLabel.stringValue = ""
    }

}

class UserMessageCell: MessageBaseCell {
    static let reuseIdentifier = NSUserInterfaceItemIdentifier("UserMessageCell")
    override func loadView() {
        super.loadView()
        bubbleView.layer?.backgroundColor = NSColor.systemBlue.cgColor
        NSLayoutConstraint.activate([
            bubbleView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            bubbleView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20),
            bubbleView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
            bubbleView.leadingAnchor.constraint(greaterThanOrEqualTo: view.leadingAnchor, constant: 60),

            messageLabel.topAnchor.constraint(equalTo: bubbleView.topAnchor, constant: 8),
            messageLabel.bottomAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 8),
            messageLabel.leadingAnchor.constraint(equalTo: bubbleView.leadingAnchor, constant: 12),
            messageLabel.trailingAnchor.constraint(equalTo: bubbleView.trailingAnchor, constant: -12),

            timeLabel.topAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 3),
            timeLabel.trailingAnchor.constraint(equalTo: bubbleView.trailingAnchor, constant: 0)
        ])
    }
}

class GuestMessageCell: MessageBaseCell {
    static let reuseIdentifier = NSUserInterfaceItemIdentifier("GuestMessageCell")
    override func loadView() {
        super.loadView()
        bubbleView.layer?.backgroundColor = NSColor.controlBackgroundColor.cgColor
        NSLayoutConstraint.activate([
            bubbleView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0),
            bubbleView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20),
            bubbleView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
            bubbleView.trailingAnchor.constraint(lessThanOrEqualTo: view.trailingAnchor, constant: 60),

            messageLabel.topAnchor.constraint(equalTo: bubbleView.topAnchor, constant: 8),
            messageLabel.bottomAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 8),
            messageLabel.leadingAnchor.constraint(equalTo: bubbleView.leadingAnchor, constant: 12),
            messageLabel.trailingAnchor.constraint(equalTo: bubbleView.trailingAnchor, constant: -12),

            timeLabel.topAnchor.constraint(equalTo: bubbleView.bottomAnchor, constant: 3),
            timeLabel.leadingAnchor.constraint(equalTo: bubbleView.leadingAnchor, constant: 0)
        ])
    }
}
